<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;

class Admin extends Controller
{
    protected $middleware = [
        'CheckAdmin'
    ];

    public function lists(Request $request)
    {
        if ($request->isAjax()) {
            $type = $request->get('type', 0, 'intval');
            $page = $request->get('page', 1, 'intval');
            $page = $page > 0 ? $page : 1;
            $limit = $request->get('limit', 10, 'intval');
            $admin = Db('admin');
            if ($type !== 0) {
                $admin->where('type', $type);
            }
            $count = $admin->count();
            $list = $admin
                ->field('admin_id,username,type,nickname,last_login_time,last_login_ip,create_time,status')
                ->page($page, $limit)
                ->order('admin_id desc')
                ->select();
            return ['code' => 0, 'data' => $list, 'count' => $count];
        } else {
            return $this->fetch();
        }
    }

    public function addAdmin(Request $request)
    {
        if ($request->isPost()) {
            $data = [];
            $data['username'] = $request->post('username', '', 'trim');
            $data['password'] = $request->post('password', '', 'trim');
            $data['type'] = $request->post('type', 1, 'intval');
            $data['nickname'] = $request->post('nickname', '', 'trim');
            $data['status'] = $request->post('status', 0, 'intval');
            $data['create_time'] = date('Y-m-d H:i:s');
            if ($data['username'] == '') {
                return ['code' => 1, 'msg' => '请填管理员账号'];
            }
            if ($data['password'] == '') {
                return ['code' => 1, 'msg' => '请填管理员登录密码'];
            }
            if ($data['nickname'] == '') {
                return ['code' => 1, 'msg' => '请填管理员真实姓名'];
            }
            $admin = new \app\admin\model\Admin($data);
            $admin->safe_code = $admin->safeCode();
            $admin->password = $admin->encrypt($admin->password);
            if (false === $admin->save()) {
                return ['code' => 1, 'msg' => '操作失败'];
            } else {
                return ['code' => 0, 'msg' => '操作成功'];
            }
        } else {
            $category = Db('category')->order('sort asc')->select();
            $this->assign('category', $category);
            $teacher = Db('teacher')->where('type', 1)->order('sort asc')->select();
            $this->assign('teacher', $teacher);
            return $this->fetch();
        }
    }

    public function editAdmin(Request $request)
    {
        if ($request->isPost()) {
            $data = [];
            $data['admin_id'] = $request->post('admin_id', 0, 'intval');
            $data['username'] = $request->post('username', '', 'trim');
            $data['password'] = $request->post('password', '', 'trim');
            $data['type'] = $request->post('type', 1, 'intval');
            $data['nickname'] = $request->post('nickname', '', 'trim');
            $data['status'] = $request->post('status', 0, 'intval');
            if ($data['username'] == '') {
                return ['code' => 1, 'msg' => '请填管理员账号'];
            }
            if ($data['nickname'] == '') {
                return ['code' => 1, 'msg' => '请填管理员真实姓名'];
            }
            $admin = \app\admin\model\Admin::where(['admin_id'=>$data['admin_id']])->find();
            if($data['password'] == ''){
                unset($data['password']);
            }
            $admin->data($data);
            if(isset($data['password'])) {
                $admin->safe_code = $admin->safeCode();
                $admin->password = $admin->encrypt($admin->password);
            }
            if (false === $admin->save()) {
                return ['code' => 1, 'msg' => '操作失败'];
            } else {
                return ['code' => 0, 'msg' => '操作成功'];
            }
        } else {
            $admin_id = $request->get('id', 0, 'intval');
            $info = Db('admin')->where('admin_id', $admin_id)->find();
            $this->assign('info', $info);
            return $this->fetch();
        }
    }

    public function deleteAdmin(Request $request)
    {
        $admin_id = $request->get('id', 0, 'intval');
        if (false === Db('admin')->where('admin_id', $admin_id)->delete()) {
            return ['code' => 1, 'msg' => '操作失败'];
        } else {
            return ['code' => 0, 'msg' => '操作成功'];
        }
    }
}